package cn.lopr.im.android.mvp.model;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import cn.lopr.im.android.bean.Article;
import cn.lopr.im.android.bean.ArticleComment;
import cn.lopr.im.android.bean.ArticleCommentVo;
import cn.lopr.im.android.bean.ArticlePlate;
import cn.lopr.im.android.bean.ArticleVo;
import cn.lopr.im.android.bean.CallbackResult;
import cn.lopr.im.android.bean.PageInfo;
import cn.lopr.im.android.common.Constant;
import cn.lopr.im.android.common.OnCallBackListener;
import cn.lopr.im.android.mvp.contact.ICommunityContact;
import cn.lopr.im.android.utils.Apis;
import cn.lopr.im.android.utils.HttpUtils;

/**
 * @author shuangyang7
 * @date 2020/12/19 11:51
 */
public class CommunityModel implements ICommunityContact.ICommunityModel {

    private static CommunityModel communityModel;
    private String TAG;

    public static CommunityModel getInstance() {
        if (communityModel == null) {
            communityModel = new CommunityModel();
        }
        return new CommunityModel();
    }

    @Override
    public void getPlates(String token, OnCallBackListener<CallbackResult<List<ArticlePlate>>, String> onCallBackListener) {

        Log.i("ContactModel", Apis.WebApis.GET_PLATE.getValue());

        HttpUtils.doGetForResult(Apis.WebApis.GET_PLATE.getValue(), "", token, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<List<ArticlePlate>>>() {
            }.getType();
            CallbackResult<List<ArticlePlate>> callbackResult = new Gson().fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void getArticleList(String token, String plateId, int page, int size, OnCallBackListener<CallbackResult<PageInfo<ArticleVo>>, String> onCallBackListener) {
        Log.i("ContactModel", Apis.WebApis.GET_ARTICLE_LIST.getValue());
        String param = String.format("?plateId=%s&page=%s&size=%s", plateId, page, size);
        HttpUtils.doGetForResult(Apis.WebApis.GET_ARTICLE_LIST.getValue(), param, token, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<PageInfo<ArticleVo>>>() {
            }.getType();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            CallbackResult<PageInfo<ArticleVo>> callbackResult = gson.fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void getArticle(String token, String articleId, OnCallBackListener<CallbackResult<ArticleVo>, String> onCallBackListener) {
        String param = String.format("?articleId=%s", articleId);
        HttpUtils.doGetForResult(Apis.WebApis.GET_ARTICLE.getValue(), param, token, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<ArticleVo>>() {
            }.getType();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            CallbackResult<ArticleVo> callbackResult = gson.fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void addArticle(String token, Article article, OnCallBackListener<CallbackResult<String>, String> onCallBackListener) {

        HttpUtils.doPostForResult(Apis.WebApis.ADD_ARTICLE.getValue(), article, token, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<String>>() {
            }.getType();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            CallbackResult<String> callbackResult = gson.fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void updateArticle(String token, Article article, OnCallBackListener<CallbackResult<String>, String> onCallBackListener) {

        HttpUtils.doPostForResult(Apis.WebApis.UPDATE_ARTICLE.getValue(), article, token, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<String>>() {
            }.getType();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            CallbackResult<String> callbackResult = gson.fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void delArticle(String token, String articleId, OnCallBackListener<CallbackResult<String>, String> onCallBackListener) {
        String param = String.format("?articleId=%s", articleId);
        HttpUtils.doGetForResult(Apis.WebApis.DEL_ARTICLE.getValue(), param, token, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<String>>() {
            }.getType();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            CallbackResult<String> callbackResult = gson.fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void getArticleComment(String token, String articleId, int page, int size, OnCallBackListener<CallbackResult<List<ArticleCommentVo>>, String> onCallBackListener) {
        Log.i("ContactModel", Apis.WebApis.GET_ARTICLE_COMMENT.getValue());
        String param = String.format("?articleId=%s&page=%s&size=%s", articleId, page, size);
        HttpUtils.doGetForResult(Apis.WebApis.GET_ARTICLE_COMMENT.getValue(), param, token, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<List<ArticleCommentVo>>>() {
            }.getType();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            CallbackResult<List<ArticleCommentVo>> callbackResult = gson.fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void getArticleChildComment(String token, String parentId, int page, int size, OnCallBackListener<CallbackResult<List<ArticleCommentVo>>, String> onCallBackListener) {
        String param = String.format("?parentId=%s&page=%s&size=%s", parentId, page, size);
        HttpUtils.doGetForResult(Apis.WebApis.GET_ARTICLE_CHILD_COMMENT.getValue(), param, token, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<List<ArticleCommentVo>>>() {
            }.getType();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            CallbackResult<List<ArticleCommentVo>> callbackResult = gson.fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void addArticleComment(String token, ArticleComment articleComment, OnCallBackListener<CallbackResult<String>, String> onCallBackListener) {
        Log.i("ContactModel", Apis.WebApis.GET_ARTICLE.getValue());

        HttpUtils.doPostForResult(Apis.WebApis.ADD_ARTICLE_COMMENT.getValue(), articleComment, token, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<String>>() {
            }.getType();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            CallbackResult<String> callbackResult = gson.fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void getArticleLike(String token, String plateId, int page, int size, OnCallBackListener<CallbackResult<List<ArticleVo>>, String> onCallBackListener) {
        Log.i("ContactModel", Apis.WebApis.GET_ARTICLE_LIKE.getValue());
        String param = String.format("?plateId=%s&page=%s&size=%s", plateId, page, size);
        HttpUtils.doGetForResult(Apis.WebApis.GET_ARTICLE.getValue(), param, token, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<List<ArticleVo>>>() {
            }.getType();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            CallbackResult<List<ArticleVo>> callbackResult = gson.fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void getArticleShare(String token, String plateId, int page, int size, OnCallBackListener<CallbackResult<List<ArticleVo>>, String> onCallBackListener) {
        Log.i("ContactModel", Apis.WebApis.GET_ARTICLE_SHARE.getValue());
        String param = String.format("?plateId=%s&page=%s&size=%s", plateId, page, size);
        HttpUtils.doGetForResult(Apis.WebApis.GET_ARTICLE.getValue(), param, token, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<List<ArticleVo>>>() {
            }.getType();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            CallbackResult<List<ArticleVo>> callbackResult = gson.fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void getLikeMeLikeYouVisitPublish(Integer userId, OnCallBackListener<CallbackResult<Map<String, Integer>>, String> onCallBackListener) {

        String param = String.format("?userId=%s", userId);
        HttpUtils.doGetForResult(Apis.WebApis.GET_LikeMe_LikeYou_Visit_Publish.getValue(), param, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<Map<String, Integer>>>() {
            }.getType();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            CallbackResult<Map<String, Integer>> callbackResult = gson.fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }

    @Override
    public void getArticleListByUserId(String token, Integer userId, int page, int size, OnCallBackListener<CallbackResult<PageInfo<ArticleVo>>, String> onCallBackListener) {
        String param = String.format("?userId=%s&page=%s&size=%s", userId, page, size);
        Log.i(TAG, "url: " + Apis.WebApis.GET_ARTICLE_LIST_BY_USER_ID.getValue());
        HttpUtils.doGetForResult(Apis.WebApis.GET_ARTICLE_LIST_BY_USER_ID.getValue(), param, token, data -> {
            Log.d("json", data);

            if (TextUtils.isEmpty(data)) {
                onCallBackListener.failure(Constant.CALLBACK_ERROR_MESSAGE);
                return;
            }

            Type jsonType = new TypeToken<CallbackResult<PageInfo<ArticleVo>>>() {
            }.getType();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            CallbackResult<PageInfo<ArticleVo>> callbackResult = gson.fromJson(data, jsonType);

            if (callbackResult.getStatus() == Constant.ResponseCode.SUCCESS.getCode()) {
                onCallBackListener.success(callbackResult);

            } else {
                onCallBackListener.failure(callbackResult.getMsg());

            }
        });
    }
}
